import 'dart:convert';

import './storage.dart';

class CheckOutService{

  /// 计算总价
  static getAllPrice(checkOutList) {
    double allPrice = 0.0;
    for(var i=0;i<checkOutList.length;i++) {
      if(checkOutList[i]['checked']) {
        allPrice += checkOutList[i]['price'] * checkOutList[i]['count'];
      }
    }
    return allPrice;
  }

  /// 删除购物车已选中的数据
  static removeSelectedCartItem() async{
    /// 获取购物车数据
    List _cartList = [];
    try {
      _cartList = json.decode(await Storage.getString('cartList'));
    } catch(e) {
      _cartList = [];
    }

    /// 筛选购物车没有选中的数据
    List tempList = [];
    for(var i=0;i<_cartList.length;i++) {
      if(!_cartList[i]['checked']) {
        tempList.add(_cartList[i]);
      }
    }
    
    /// 保存购物车没有选中的数据
    Storage.setString('cartList', json.encode(tempList));
  }

}